package rz.dmc.effect;

import java.util.ArrayList;


import rz.dmc.util.RZChildViewInfo;
import rz.dmc.util.RZFrame;
import rz.util.RZLog;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class RZOverlayViewGroup extends RelativeLayout 
{
	private Context	 context;
	private ArrayList<RZChildViewInfo> childViewInfoList = new ArrayList<RZChildViewInfo>();

	private int beginX;
	private int beginY;
	private int endX;
	private int endY;
	private int moveX;
	private int moveY;
	
	
	public RZOverlayViewGroup(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		this.context = context;  
		this.setBackgroundColor(Color.CYAN);
	}

	public void addView(View view, RZFrame frame)
	{
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)frame.width, (int)frame.height);
		layoutParams.leftMargin = (int)frame.x;
		layoutParams.topMargin = (int)frame.y;
		layoutParams.bottomMargin = 0;
		layoutParams.rightMargin = 0;
		
		RZChildViewInfo childViewInfo = new RZChildViewInfo(view, frame);
		childViewInfo.view.setOnTouchListener(subViewOnTouchListener);
		childViewInfo.view.setLayoutParams(layoutParams);

		childViewInfoList.add(childViewInfo);
		
		addView(childViewInfo.view, (int)childViewInfo.frame.width, (int)childViewInfo.frame.height);
	}

//	@Override
//	protected void onLayout(boolean changed, int l, int t, int r, int b)
//	{
//		RZLog.logInfo("RZOverlayView | onLayout");
//		
//		int childCount = getChildCount();
//		
//		for (int i=0; i<childCount; i++)
//		{
//			View tempChildView = getChildAt(i);
//			
//			if (tempChildView == null)
//				continue;
//			
//			
//			for (RZChildViewInfo childViewInfo : childViewInfoList)
//			{
//				View currentChildView = childViewInfo.view;
//				
//				if (tempChildView.equals(currentChildView))
//				{
//					RZFrame frame = childViewInfo.frame;
//					currentChildView.layout((int)frame.x, (int)frame.y, (int)frame.width, (int)frame.height);
//				}
//			}
//		}
//	}
	
	private OnTouchListener subViewOnTouchListener = new OnTouchListener()
	{
		@Override
		public boolean onTouch(View view, MotionEvent event)
		{
			final int x = (int) event.getRawX();
			final int y = (int) event.getRawY();

			switch (event.getAction() & MotionEvent.ACTION_MASK) 
	        {
	            case MotionEvent.ACTION_DOWN:
	            {
	            	beginX = x;
	            	beginY = y;
	            	
	            	onTouchBegin(x, y);
	            	
	            	RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
					moveX = x - layoutParams.leftMargin;
					moveY = y - layoutParams.topMargin;
	            }
	                break;
	
	            case MotionEvent.ACTION_MOVE:
	            {
	            	onTouchMove(x, y);
	            	
	            	RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
					layoutParams.leftMargin = x - moveX;
					layoutParams.topMargin = y - moveY;
					layoutParams.rightMargin = -250;
					layoutParams.bottomMargin = -250;
					view.setLayoutParams(layoutParams);
	            }
	                break;
	
	            case MotionEvent.ACTION_UP:
	            {
	            	endX = x;
	            	endY = y;
	            	
	            	onTouchEnd(x, y);
	            	
	            	// 터치 이동이 없으면, onTouchListener 무시!
	            	if (endX == beginX && endY == beginY)
	        			return false;
	            }
	            	break;
	                
	            default:
	            	RZLog.logInfo("Ignored touch event : " + event.toString());
	                return false;
	        }
			
	        return true;
		}
	};
	
	private void onTouchBegin(float x, float y)
	{
		RZLog.logInfo("RZOverlayViewGroup | onTouchBegin | x:" + x + ", y:" + y);
	}	

	private void onTouchMove(float x, float y)
	{
		RZLog.logInfo("RZOverlayViewGroup | onTouchMove | x:" + x + ", y:" + y);
	}

	private void onTouchEnd(float x, float y)
	{
		RZLog.logInfo("RZOverlayViewGroup | onTouchEnd");
	}
	
}
